<template>
	<view class="main">
		<!-- 手机导航栏占位 -->
		<view class="mainHead"></view>
		<!-- 登录上方菜单栏 -->
		<view class="loginTop">
			<view class="loginTopCode" v-if="loginMode == 2" @click="loginCode">手机号登录</view>
			<view class="loginTopCode" v-if="loginMode == 1" @click="loginCode">验证码登录</view>
		</view>
		<!-- 登录框 -->
		<view class="loginBox">
			<view class="loginInptu">
				<input type="number" class="username" value="" placeholder="请输入手机号" maxlength="11" @input="phoneInput" />
			</view>
			<view class="loginInptu" v-if="loginMode == 1">
				<input type="password" class="password" value="" placeholder="请输入密码" @input="passwordInpur" />
			</view>
			<view class="loginInptu loginInptuCode" v-else-if="loginMode == 2">
				<input type="number" class="code" value="" placeholder="请输入验证码" maxlength="4" @input="codeInput" />
				<view v-if="isPhone==false" class="huoquyanzhengma" @click="getCode">{{codeBut}}</view>
				<view v-if="isPhone==true" class="huoquyanzhengma" style="font-size:x-small;">{{timeLeft}}s后重新获取</view>
			</view>
			<view style="color: #FF0000; height: 50rpx;margin-left: auto;">{{errorMessage}}</view>
			<view class="loginUp" @click="loginUp">
				<image style="height: 80upx;width: 100upx;" src="/static/login.png"></image>
			</view>
		</view>

	</view>
</template>

<script>
	import base from '@/common/base.js'
	export default {
		data() {
			return {
				loginMode: 1,
				usernameType: "text",
				codeBut: "获取验证码",
				codeClick: true,
				phone: "",
				password: "",
				errorMessage: "",
				code: "",
				isPhone: false,
				timeLeft: 120
			}
		},
		onLoad() {
			this.getCoachInfo();
		},
		methods: {
			getCoachInfo() {
				uni.request({
					url: base.urlPrefix + 'yoga_app/coach-info/getCoachInfoInSession',
					success: (res) => {
						if (res.data.code == "success") {
							base.coach = res.data.object;
							uni.switchTab({
								url: "/pages/tabBar/home/home",
								success() {
									let page = getCurrentPages().pop();
									page.onLoad();
								}
							});
						}
					}
				})
			},
			loginCode() {
				if (this.loginMode == 1) {
					this.loginMode = 2
				} else {
					this.loginMode = 1
				}
				this.usernameType = "number"
			},
			loginPhone() {
				this.loginMode = 1
				this.usernameType = "text"
			},
			// 手机号输入
			phoneInput(e) {
				this.phone = e.detail.value
			},
			// 密码输入
			passwordInpur(e) {
				this.password = e.detail.value
			},
			// 验证码输入
			codeInput(e) {
				this.code = e.detail.value
			},
			// 点击登录
			loginUp() {
				var _that = this
				if (_that.phone == "") {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none',
						duration: 1000
					});
				} else if (_that.password == "" && _that.loginMode == 1) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none',
						duration: 1000
					});
				} else if (_that.code == "" && _that.loginMode == 2) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 1000
					});
				} else {
					if (_that.loginMode == 1) {
						uni.request({
							url: base.urlPrefix + 'yoga_app/coach-info/coachLoginByPassword',
							data: {
								tel: this.phone,
								password: this.password
							},
							success: (res) => {
								if (res.data.code == "success") {
									uni.showToast({
										title: res.data.message,
										icon: 'none'
									});
									this.errorMessage = '';
									base.coach = res.data.object;
									uni.switchTab({
										url: "/pages/tabBar/home/home",
										success() {
											let page = getCurrentPages().pop();
											page.onLoad();
										}
									});
								} else {
									this.errorMessage = res.data.message;
								}
							}
						})
					} else {
						if (this.isPhone == true) {
							uni.request({
								url: base.urlPrefix + 'yoga_app/coach-info/coachLoginByCode',
								data: {
									code: this.code
								},
								success: (res) => {
									if (res.data.code == "success") {
										uni.showToast({
											title: res.data.message,
											icon: 'none'
										});
										this.errorMessage = '';
										base.coach = res.data.object;
										uni.switchTab({
											url: "/pages/tabBar/home/home",
											success() {
												let page = getCurrentPages().pop();
												page.onLoad();

											}
										});
									} else {
										this.errorMessage = res.data.message;
									}
								}
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '请先获取验证码'
							})
						}
					}
				}
			},
			// 获取验证码
			getCode() {
				if (RegExp(/^1[34578]\d{9}$/).test(this.phone)) {
					uni.request({
						url: base.urlPrefix + 'yoga_app/coach-info/coachGetCheckNum',
						data: {
							tel: this.phone
						},
						success: (res) => {
							this.errorMessage = res.data.message;
							if (res.data.code == "success") {
								this.isPhone = true;
								let interval = setInterval(() => {
									--this.timeLeft;
								}, 1000)
								setTimeout(() => {
									clearInterval(interval);
									this.timeLeft = 120;
								}, 12000);
							}
						}

					});
				} else {
					this.errorMessage = "请输入正确的手机号";

				}
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
	}

	.main {
		background: #4191ea;
		width: 100%;
		height: 100%;
	}

	.mainHead {
		height: var(--status-bar-height);
	}

	.loginTop {
		width: 700upx;
		margin: 0 auto;
		height: 40upx;
		line-height: 40upx;
		display: flex;
		justify-content: space-between;
		font-size: 30upx;
		margin-top: 10upx;
	}

	.loginTopBack {
		width: 35upx;
		height: 35upx;
	}

	.loginUp {
		width: 100upx;
		height: 100upx;
		margin: 0 auto;
		padding: 15upx;
		border: 1upx solid #333333;
		border-radius: 60upx;
		margin-top: 60upx;
		background: #ffffff;
	}

	.code {
		background: url(/static/code.png)no-repeat;
		background-position: 10upx;
		padding-left: 65upx;
		width: 300upx;
		background-size: 58upx;
	}

	.huoquyanzhengma {
		background: #333333;
		color: #fff;
		line-height: 85upx;
		width: 250upx;
		text-align: center;
		border-radius: 10upx;
	}

	.loginBox {
		width: 650upx;
		margin: 60upx auto;
	}

	.username {
		background: url(/static/shouji.png)no-repeat;
		background-position: 10upx;
		padding-left: 65upx;
	}

	.password {
		background: url(/static/mima.png)no-repeat;
		background-position: 5upx;
		padding-left: 70upx;
	}

	.loginInptu {
		width: 500upx;
		height: 80upx;
		margin: 0 auto;
		background: #fff;
		margin-top: 20upx;
		line-height: 80upx;
		border-radius: 10upx;
	}

	.loginInptuCode {
		display: flex;
	}

	.loginInptu>input {
		height: 100%;
		font-size: 30upx;
	}

	.logoImgBox {
		width: 200upx;
		height: 200upx;
		margin: 0 auto;
		margin-bottom: 50upx;
	}

	.otherLogin {
		width: 500upx;
		height: 95upx;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
	}

	.otherLogin>view {
		width: 95upx;
		height: 95upx;
		background: #fff;
		border-radius: 95upx;
	}

	.otherLoginTitle {
		width: 100%;
		text-align: center;
		color: #555555;
		font-size: 28upx;
		line-height: 80upx;
	}

	.bottomBox {
		display: flex;
		justify-content: center;
		position: fixed;
		bottom: 60upx;
		left: 0;
		right: 0;
		margin: auto;
		height: 30upx;
		line-height: 30upx;
		font-size: 32upx;
	}

	.wangjimima {
		border-right: 1rpx solid #555;
		height: 25upx;
		padding-right: 15upx;
	}

	.yonghuzhuce {
		padding-left: 15upx;
	}
</style>
